<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <select class="province">
        <option value="">请选择省份</option>
        <!-- <option value="A001">北京</option>
        <option value="A002">上海</option>
        <option value="A003">湖北</option>
        <option value="A004">湖南</option> -->
    </select>
    <select class="city" name="" id="">
        <option value="">请选择市/区</option>
        <!-- <option value="A001">朝阳</option>
        <option value="A002">海淀</option>
        <option value="A003">沙河</option>
        <option value="A004">上地</option> -->
    </select>

</body>
<script>
    var province = document.querySelector(".province");
    var option = province.getElementsByTagName("option");
    var city = document.querySelector(".city");

    var xhr = new XMLHttpRequest();
    xhr.open("get", "../data/pro.json", true);
    xhr.send();
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            var obj = JSON.parse(xhr.responseText); //获取JSON对象
            // console.log(obj);
            var { list, list: [] } = obj; //第一次解构，解构对象
            // console.log(list);
            citySwitch(list, province); //调用函数

            var [a, b, c, d] = list; //第二次解构，解构数组
            var { city_id, city_name, list } = a; //第三次解构，解构对象
            citySwitch(list, city); //加载页面时默认执行第一个
            province.onchange = function () {
                // console.log(province.value);
                if (province.value == "A001") { //如果选择了第一个就执行第一个
                    var { city_id, city_name, list } = a;
                } else if (province.value == "A002") {
                    var { city_id, city_name, list } = b;
                } else if (province.value == "A003") {
                    var { city_id, city_name, list } = c;
                } else if (province.value == "A004") {
                    var { city_id, city_name, list } = d;
                }
                citySwitch(list, city);
            }

        }
    }


    function citySwitch(list, city) {
        var html = "";
        list.forEach(function (item) {
            var { city_id, city_name } = item;
            html += `<option value="${city_id}">${city_name}</option>`;
            city.innerHTML = html;
        });
    }

</script>

</html>